package com.bwie.controller;

import com.bwie.pojo.TbActive;
import com.bwie.service.TbActiveService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2025/7/2 11:37
 */

@RestController
@RequestMapping("/win/index")
public class IndexController {

    @Autowired
    TbActiveService tbActiveService;

    @Autowired
    RedissonClient redissonClient;

    /**
     * @description 分页获取抽奖活动列表
     * @params [pageInfoVo]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/7/2 11:40
     */
    @PostMapping("/getActiveList")
    public R getActiveList(@RequestBody PageInfoVo pageInfoVo) {
        return tbActiveService.getActiveList(pageInfoVo);
    }

    /**
     * @description 添加抽奖活动
     * @params []
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/7/3 14:10
     */
    @PostMapping("/addActive")
    public R addActive(@RequestBody @Validated TbActive tbActive) {
        return tbActiveService.addActive(tbActive);
    }

    /**
     * @description 用户免费参与抽奖
     * @params [activeId]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/7/4 18:51
     */
    @PostMapping("/joinActive/{activeId}")
    public R joinActive(@PathVariable("activeId") Integer activeId) {
        return tbActiveService.joinActive(activeId);
    }

    /**
     * @description 用户积分抽奖
     * @params [activeId]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/7/7 11:52
     */
    @PostMapping("/joinActiveByScore/{activeId}")
    public R joinActiveByScore(@PathVariable("activeId") Integer activeId) {
        return tbActiveService.joinActiveByScore(activeId);
    }


    /**
     * @description 获取活动的抽奖记录
     * @params [activeId]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/7/5 9:55
     */
    @PostMapping("/getJoinRecord")
    public R getJoinRecord(@RequestBody PageInfoVo pageInfoVo) {
        return tbActiveService.getJoinRecord(pageInfoVo);
    }

    @PostMapping("/getTestRedis")
    public R getTestRedis() {
        RLock rLock = redissonClient.getLock("hahaha");
        rLock.lock();
        rLock.unlock();

        return R.ok();
    }

    /**
     * @description 根据活动ID获取活动的信息
     * @params [activeId]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/7/7 14:39
     */
    @PostMapping("/getActiveById/{activeId}")
    public R getActiveById(@PathVariable("activeId") Integer activeId) {
        return tbActiveService.getActiveById(activeId);
    }

}
